package com.dbh.alg.algorithm.quest;

public class BitMap {

    private byte[] bits;

    private int size;

    public BitMap(int size) {
        this.size = size;
        this.bits = new byte[(size + 7) / 8];
    }

    public void set(int index) {
        if(index < 0 || index > size) {
            throw new IndexOutOfBoundsException("index out of bounds");
        }
        bits[index / 8] |= (byte) (1 << (index % 8));
    }

    public  boolean get(int index) {
        if(index < 0 || index > size) {
            throw new IndexOutOfBoundsException("index out of bounds");
        }
        return (bits[index / 8] &  (1 << (index % 8))) != 0;
    }

    public void clear(int index) {
        if(index < 0 || index > size) {
            throw new IndexOutOfBoundsException("index out of bounds");
        }
        bits[index / 8] &= (byte) ~(1 << (index % 8));
    }

    public static void main(String[] args) {
        BitMap bitMap = new BitMap(16);
        bitMap.set(10);
        System.out.println(bitMap.get(9));
        System.out.println(bitMap.get(10));
        bitMap.clear(10);
        System.out.println(bitMap.get(10));
    }

}
